import { IControlInstance, IEntityInstance } from "@/typings/scenario-edit";

/**
 * description: 新增实体/控制对象 初始化名称
 * fileName: NameUtils.ts
 * author: ssr
 * date: 2023-01-12 15:37:04
 * version: V1.0.0
 */
export default class NameUtils {
  /**
   * 初始化实体实例名称
   * @param list 已有实例
   * @param assistIdentify
   * @returns
   */
  static getInstanceName(
    list: IEntityInstance[],
    assistIdentify: string
  ): string {
    let max = 0;
    list.forEach((item) => {
      let eName = item.basicInfo.entityName;
      if (eName && eName.indexOf(assistIdentify) !== -1) {
        let str: string[] = eName.split(" ");
        if (str.length > 0) {
          max =
            Number(str[str.length - 1]) > max
              ? Number(str[str.length - 1])
              : max;
        }
      }
    });
    return assistIdentify + " " + ++max;
  }

  /**
   * 初始化控制对象实例名称
   * @param list 已有控制对象实例
   * @param type
   * @returns
   */
  static getControlName(list: IControlInstance[], name: string): string {
    let max = 0;
    list.forEach((item) => {
      let eName = item.name;
      if (eName && eName.indexOf(name) !== -1) {
        let str: string[] = eName.split(" ");
        if (str.length > 0) {
          max =
            Number(str[str.length - 1]) > max
              ? Number(str[str.length - 1])
              : max;
        }
      }
    });
    return name + " " + ++max;
  }
}
